package com.example.demo.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Console;
import com.example.demo.constant.Constant;
import com.example.demo.model.CaptchaProperties;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Locale;

@RestController
@RequestMapping("/captcha")
public class CaptchaController {
    @Value("${timeout}")
    private Long timeout;
    private CaptchaProperties captchaProperties;

    public CaptchaController() {
    }

    @Autowired
    public CaptchaController(CaptchaProperties captchaProperties) {
        this.captchaProperties = captchaProperties;
    }

    @RequestMapping("/get")
    public void getCaptcha(HttpServletResponse response, HttpSession session){
        try(OutputStream outputStream = response.getOutputStream()){
            // 1. 生成一张验证码
            //    定义图形验证码的长和宽
            ICaptcha captcha = CaptchaUtil.createLineCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight());
            // 2. 设置返回类型
            response.setContentType("image/png");
            //    禁止缓存
            response.setHeader("Progma", "No-cache");
            // 3. 图形验证码写出，可以写出到文件，也可以写出到流
            captcha.write(outputStream);
            // 4. 在会话中，保存验证码的值，方便后续校验时拿到
            String captchaCode = captcha.getCode();
            session.setAttribute(captchaProperties.getSession().getCaptchaCode(), captchaCode);
            // 5. 生成校验码的时候，记录生成时间，便于后续进行超时判断
            Date captchaTime = new Date(System.currentTimeMillis() + timeout);
            session.setAttribute(captchaProperties.getSession().getCaptchaTime(), captchaTime);
            //Servlet的OutputStream记得自行关闭哦！

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @RequestMapping("/check")
    public Boolean checkCaptcha(String userInput, HttpSession session){
        // 1. 参数校验
        if(!StringUtils.hasLength(userInput)){
            return false;
        }
        // 2. 检查过期时间
        Date inputTime = new Date(System.currentTimeMillis());
        Date captchaTime = (Date) session.getAttribute(captchaProperties.getSession().getCaptchaTime());
        if(inputTime.after(captchaTime)){
            return false;
        }
        // 3. 对比生成的验证码
        //    考虑到线程安全的问题，生成的验证码不能单独定义属性保存，推荐保存在会话中
        String captchaCode = (String) session.getAttribute(captchaProperties.getSession().getCaptchaCode());
        if(!userInput.equalsIgnoreCase(captchaCode)){
            return false;
        }

        return true;
    }
}
